package cn.rengy.lang;



import lombok.ToString;

import java.io.Serializable;
/**
 * 通用结果返回
 * @author rengy
 * 
 * @param <C,T>
 */
@ToString
public class ResultEntity<C,T> implements Serializable {

	private static final long serialVersionUID = 1L;
	protected Boolean success;
	protected C code;
	protected String message;
	protected T data;
	/**
	 * 消耗时间
	 */
	protected Integer spendTime;
	public ResultEntity() {
		
	}
	public ResultEntity(Boolean success,C code, String message, T data) {
		this.success = success;
		this.code = code;
		this.message = message;
		this.data = data;
	}
	
	public static <C,T> ResultEntity<C,T> ok() {
		return new ResultEntity<C,T>(Boolean.TRUE, (C) Integer.valueOf(0), "ok", null);
	}
	public static <C,T> ResultEntity<C,T> ok(T body) {
		if(body instanceof ResultEntity){
			throw new RuntimeException("ResultEntity的data不能设置为ResultEntity");
		}
		return new ResultEntity<C,T>(Boolean.TRUE,(C) Integer.valueOf(0), "ok", body);
	}
	public static <C,T> ResultEntity<C,T> ok(C code,T body) {
		if(body instanceof ResultEntity){
			throw new RuntimeException("ResultEntity的data不能设置为ResultEntity");
		}
		return new ResultEntity<C,T>(Boolean.TRUE,code, "ok", body);
	}

	public static <C,T> ResultEntity<C,T> fail() {
		return new ResultEntity<C,T>(Boolean.FALSE,(C) Integer.valueOf(1), "fail", null);
	}
	public static <C,T> ResultEntity<C,T> fail(String message) {
		return new ResultEntity<C,T>(Boolean.FALSE,(C) Integer.valueOf(1), message, null);
		
	}
	
	public static <C,T> ResultEntity<C,T> fail(C code,String message) {
		return new ResultEntity<C,T>(Boolean.FALSE,code, message, null);
		
	}
	
	public Boolean isSuccess() {
		return success;
	}

	public Boolean getSuccess() {
		return success;
	}

	public Integer getSpendTime() {
		return spendTime;
	}

	public void setSpendTime(Integer spendTime) {
		this.spendTime = spendTime;
	}

	public ResultEntity<C,T> setSuccess(Boolean success) {
		this.success = success;
		return this;
	}


	public C getCode() {
		return code;
	}


	public ResultEntity<C,T> setCode(C code) {
		this.code = code;
		return this;
	}


	public String getMessage() {
		return message;
	}


	public ResultEntity<C,T> setMessage(String message) {
		this.message = message;
		return this;
	}


	public T getData() {
		return data;
	}


	public ResultEntity<C,T> setData(T data) {
		this.data = data;
		return this;
	}

}
